package model.data.database;

/**
 * AccountTable.java
 * Purpose: Represents an account.
 * 
 * @author Hans Brems
 * @version 0.2 24/01/2012
 */

import java.util.ArrayList;
import java.util.List;

import model.Account;
import model.data.AccountStore;

public class AccountTable implements AccountStore {

	/* Handles requests to the database */
	DatabaseHandler dbHandler;

	/** Constructs a new account table */
	public AccountTable() {
		this.dbHandler = new DatabaseHandler();
	}

	/** Writes a new account to the table */
	@Override
	public void write(Account account) throws Exception {
		this.dbHandler.callUpdateProcedure("sp_insertaccount(?,?,?)",
				account.getUserName(), account.getPassword(), account.getAdmin());
	}

	/** Updates an existing account in the table */
	@Override
	public void update(Account account) {
		// TODO Auto-generated method stub
	}

	/** Deletes an account from the table */
	@Override
	public void delete(Account account) throws Exception {
		this.dbHandler.callUpdateProcedure("sp_deleteaccount(?)", account.getId());
	}

	/** Returns the numbers of records in the table */
	@Override
	public int getCount() throws Exception {
		int count = 0;
		ArrayList<DataRecord> records;

		// Get records
		records = (ArrayList<DataRecord>) this.dbHandler.callQueryProcedure("sp_getcountaccount");

		// Fill list
		for (DataRecord dr : records) {
			count = Integer.parseInt(dr.getValue("Result"));
		}

		// Return result
		return count;

	}

	/** Returns an account by ID number */
	@Override
	public List<Account> getById(long id) throws Exception {
		ArrayList<Account> accounts;
		ArrayList<DataRecord> records;
		
		// Get records
		records = (ArrayList<DataRecord>)this.dbHandler.callQueryProcedure("sp_getbyidaccount(?)", id);
		
		// Create list
		accounts = new ArrayList<Account>();
		
		// Fill list
		for (DataRecord dr : records) {
			Account account = new Account();
			account.setId(Long.parseLong(dr.getValue("AccountID")));
			account.setUserName(dr.getValue("UserName"));
			account.setPassword(dr.getValue("Password"));
			account.setAdmin(Boolean.parseBoolean(dr.getValue("Admin")));
			accounts.add(account);
		}
		
		// Return result
		return accounts;
	}

	/** Returns an account by user name */
	@Override
	public List<Account> getByUserName(String userName) throws Exception {
		ArrayList<Account> accounts;
		ArrayList<DataRecord> records;
		
		// Get records
		records = (ArrayList<DataRecord>)this.dbHandler.callQueryProcedure("sp_getbynameaccount(?)", userName);
		
		// Create list
		accounts = new ArrayList<Account>();
		
		// Fill list
		for (DataRecord dr : records) {
			Account account = new Account();
			account.setId(Long.parseLong(dr.getValue("AccountID")));
			account.setUserName(dr.getValue("UserName"));
			account.setPassword(dr.getValue("Password"));
			account.setAdmin(Boolean.parseBoolean(dr.getValue("Admin")));
			accounts.add(account);
		}
		
		// Return result
		return accounts;
	}
	
}